package cn.edu.zafu.bigdata.hdb;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class StudentManagementSystem {
    private List<Student> students;
    private final String FILE_NAME = "students.dat";

    public StudentManagementSystem() {
        students = loadStudents();
    }

    private List<Student> loadStudents() {
        List<Student> studentList = new ArrayList<>();
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME))) {
            studentList = (List<Student>) ois.readObject();
        } catch (FileNotFoundException e) {
            // 文件不存在，返回空的学生列表
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return studentList;
    }

    private void saveStudents() {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_NAME))) {
            oos.writeObject(students);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void addStudent() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        for (Student student : students) {
            if (student.getId().equals(id)) {
                System.out.println("学号重复，请重新输入。");
                return;
            }
        }
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();
        students.add(new Student(id, name, phone));
        saveStudents();
        System.out.println("学生信息添加成功。");
    }

    private void searchStudent() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("请输入学号进行检索，输入QUIT退出: ");
            String id = scanner.nextLine();
            if (id.equalsIgnoreCase("QUIT")) {
                break;
            }
            boolean found = false;
            for (Student student : students) {
                if (student.getId().equals(id)) {
                    System.out.println(student);
                    found = true;
                    break;
                }
            }
            if (!found) {
                System.out.println("未找到该学生。");
            }
        }
    }

    private void updateStudent() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("请输入学号进行更新，输入QUIT退出: ");
            String id = scanner.nextLine();
            if (id.equalsIgnoreCase("QUIT")) {
                break;
            }
            for (Student student : students) {
                if (student.getId().equals(id)) {
                    System.out.print("请输入新姓名: ");
                    student.setName(scanner.nextLine());
                    System.out.print("请输入新电话: ");
                    student.setPhone(scanner.nextLine());
                    saveStudents();
                    System.out.println("学生信息更新成功。");
                    return;
                }
            }
            System.out.println("未找到该学生。");
        }
    }

    private void deleteStudent() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("请输入学号进行删除，输入QUIT退出: ");
            String id = scanner.nextLine();
            if (id.equalsIgnoreCase("QUIT")) {
                break;
            }
            boolean found = false;
            for (Student student : students) {
                if (student.getId().equals(id)) {
                    students.remove(student);
                    saveStudents();
                    found = true;
                    System.out.println("学生信息删除成功。");
                    break;
                }
            }
            if (!found) {
                System.out.println("未找到该学生。");
            }
        }
    }

    public void start() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 清除输入缓冲区
            switch (choice) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    searchStudent();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    deleteStudent();
                    break;
                case 5:
                    saveStudents();
                    System.out.println("程序结束。");
                    return;
                default:
                    System.out.println("无效选择，请重新输入。");
            }
        }
    }

    public static void main(String[] args) {
        StudentManagementSystem system = new StudentManagementSystem();
        system.start();
    }
}
